package io.framework.modules.hpinfo.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import io.framework.common.annotation.LogOperation;
import io.framework.common.constant.Constant;
import io.framework.common.exception.ErrorCode;
import io.framework.common.page.PageData;
import io.framework.common.utils.Result;
import io.framework.common.validator.AssertUtils;
import io.framework.common.validator.ValidatorUtils;
import io.framework.common.validator.group.AddGroup;
import io.framework.common.validator.group.DefaultGroup;
import io.framework.common.validator.group.UpdateGroup;
import io.framework.modules.hpinfo.dto.VTHpglProjectinfoDTO;
import io.framework.modules.hpinfo.service.VTHpglProjectinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 环境影响评价管理-环评建设项目信息
 *
 * @author Gujq email
 * @since 1.0.0 2020-03-19
 */
@RestController
@Api(tags="环境影响评价管理-环评建设项目信息")
public class VTHpglProjectinfoController {

    @Autowired
    private VTHpglProjectinfoService vTHpglProjectinfoService;


    @GetMapping("hpinfo/vthpglprojectinfo/page")
    @ApiOperation("分页")
    @ApiImplicitParams({
        @ApiImplicitParam(name = Constant.PAGE, value = "当前页码，从1开始", paramType = "query", required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.LIMIT, value = "每页显示记录数", paramType = "query",required = true, dataType="int") ,
        @ApiImplicitParam(name = Constant.ORDER_FIELD, value = "排序字段", paramType = "query", dataType="String") ,
        @ApiImplicitParam(name = Constant.ORDER, value = "排序方式，可选值(asc、desc)", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "projectXmmc", value = "项目名称", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "pffsName", value = "排放方式   直接排放、间接排放、不排放    （多个以英文逗号”,“隔开）", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "ifXkz", value = "是否有许可证  是  否 ", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "ifWry", value = "是否有污染源  是  否 ", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "hpspbm", value = "环评审批部门", paramType = "query", dataType="String"),
        @ApiImplicitParam(name = "regionName", value = "区域   天涯区  吉阳区 海棠区 崖州区   （多个以英文逗号”,“隔开）", paramType = "query", dataType="String"),
    })
    @RequiresPermissions("hpinfo:vthpglprojectinfo:page")
    public Result<PageData<VTHpglProjectinfoDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params){

        PageData<VTHpglProjectinfoDTO> page = vTHpglProjectinfoService.pages(params);

        return new Result<PageData<VTHpglProjectinfoDTO>>().ok(page);
    }

    @GetMapping("hpinfo/vthpglprojectinfo/getSpjgList")
    @ApiOperation("获取所有审批机构")
    public Result<List<Map<String,Object>>> getSpjgList(){

        List<Map<String,Object>> data = vTHpglProjectinfoService.getSpjgList();

        return new Result<List<Map<String,Object>>>().ok(data);
    }

    @GetMapping("hpinfo/vthpglprojectinfo/getHpglProjectCount")
    @ApiOperation("根据环评日期统计环评建设项目")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "year", value = "年份 -例： 2020", paramType = "query", required = true, dataType = "int"),
    })
    public Result<List<Map<String,Object>>> getHpglProjectCount(@ApiIgnore @RequestParam Map<String, Object> params){

        List<Map<String,Object>> list = vTHpglProjectinfoService.getHpglProjectCount(params);

        return new Result<List<Map<String,Object>>>().ok(list);
    }

    @GetMapping("hpinfo/vthpglprojectinfo/getHpglProjectTableCount")
    @ApiOperation("环评建设项目表格显示  个报告书、报告表 个数")
    public Result<List<Map<String,Object>>> getHpglProjectTableCount(@ApiIgnore @RequestParam Map<String, Object> params){

        List<Map<String,Object>> list = vTHpglProjectinfoService.getHpglProjectTableCount(params);

        return new Result<List<Map<String,Object>>>().ok(list);
    }


    @GetMapping("hpinfo/vthpglprojectinfo/{id}")
    @ApiOperation("查询环评信息")
    public Result<VTHpglProjectinfoDTO> get(@PathVariable("id") String id){

        VTHpglProjectinfoDTO data = vTHpglProjectinfoService.selectById(id);

        return new Result<VTHpglProjectinfoDTO>().ok(data);
    }

    @GetMapping("hpinfo/vthpglprojectinfo/getAllInfoToSelect")
    @ApiOperation("获取所有建设项目名称  --下拉框数据")
    public Result<List<Map<String, Object>>> getAllInfoToSelect(@ApiIgnore @RequestParam Map<String, Object> params) {

        List<Map<String, Object>> list= vTHpglProjectinfoService.getAllInfoToSelect(params);

        return new Result<List<Map<String, Object>>>().ok(list);
    }

    @PostMapping("hpinfo/vthpglprojectinfo/save")
    @ApiOperation("保存")
    @LogOperation("添加环评信息")
    @RequiresPermissions("hpinfo:vthpglprojectinfo:save")
    public Result save(VTHpglProjectinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        dto.setPkId(IdWorker.get32UUID());
        dto.setCreatetime(new Date());
        dto.setDataSource("手动录入");
        if(StringUtils.isNotEmpty(dto.getXygcpwxkzbh())){
            dto.setIfXkz("1");
        }
        vTHpglProjectinfoService.save(dto);

        return new Result();
    }

    @PutMapping("hpinfo/vthpglprojectinfo/update")
    @ApiOperation("修改")
    @LogOperation("修改环评信息")
    @RequiresPermissions("hpinfo:vthpglprojectinfo:update")
    public Result update(VTHpglProjectinfoDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        dto.setUpdatedate(new Date());
        if(StringUtils.isNotEmpty(dto.getXygcpwxkzbh())){
            dto.setIfXkz("1");
        }
        vTHpglProjectinfoService.update(dto);

        return new Result();
    }

    @DeleteMapping("hpinfo/vthpglprojectinfo/delete/{id}")
    @ApiOperation("删除")
    @LogOperation("删除环评信息")
    @RequiresPermissions("hpinfo:vthpglprojectinfo:delete")
    public Result delete(@PathVariable("id") String id){
        //效验数据
        AssertUtils.isBlank("id", ErrorCode.IDENTIFIER_NOT_NULL);

        vTHpglProjectinfoService.deleteById(id);

        return new Result();
    }

}